package fi.vamk.android;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class timetable extends ListActivity {
	TextView selection;
	private String[] stringIdArray; 
	private String[] stringNameArray;
	
	@Override
    public void onCreate(Bundle icicle) {
    	super.onCreate(icicle);
        setContentView(R.layout.timetable);
       
        		
        String jsonString = menuList.getDay; 
        JSONArray json = null;
        try {
            json = new JSONArray(jsonString);
        } catch (JSONException je) {
            Log.e("TAG", "Json Exception" + je.getMessage() );
            return;
        }
     JSONObject jsonObject = null;
     ArrayList<String> arrayIdList = new ArrayList<String>();
     ArrayList<String> arrayNameList = new ArrayList<String>();
     for (int i = 0; i < json.length(); i++) {
            try {
                jsonObject = json.getJSONObject(i);
                    if(jsonObject.has("name"))
                     {
                       String id = jsonObject.getString("id");
                       String name = jsonObject.getString("name");
                       arrayIdList.add(id);
                       arrayNameList.add(name);
                     }     

                 
            } catch (JSONException jee) {
                Log.e("TAG", "" + jee.getMessage());
            }

        }
        stringIdArray = arrayIdList.toArray(new String[arrayIdList.size()]);
        stringNameArray = arrayNameList.toArray(new String[arrayNameList.size()]);
        try {
	    	 
	    	 setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringNameArray));
	        		 selection=(TextView)findViewById(R.id.selection);
		} catch (Exception e) {
			// TODO: handle exception
			alertOkOnly("Exception!"  , e.toString());
		}
    
    }
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.layout.menu_button, menu);
	        return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Handle item selection
	        switch (item.getItemId()) {
	        case R.id.exit:
	        	finish();
	            return true;
	        case R.id.list:
	        	startActivity(new Intent(timetable.this, menuList.class));
	         	finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
     public void onListItemClick( ListView parent, View v, int position, long id) {
     		try {
     			dayTimetable(stringNameArray[position].toString(), Integer.parseInt(stringIdArray[position].toString()), menu.userId);
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    }
     public void dayTimetable(String title,  int dayId, int userId)
     {
    	//alertOkOnly("text", title.toString() + dayId + userId);
    	final String[] stringListArray;
    	String getTimetable = JSONParser.getJSONFromUrl(menu.IpAddress + "timetable.php?myid="+userId+"&day="+dayId ).toString(); 
    	//alertOkOnly("text", getTimetable.toString());
    	String jsonString = getTimetable; 
        JSONArray json = null;
        try {
            json = new JSONArray(jsonString);
        } catch (JSONException je) {
            Log.e("TAG", "Json Exception" + je.getMessage() );
            return;
        }
     JSONObject jsonObject = null;
     ArrayList<String> arrayList = new ArrayList<String>();
     for (int i = 0; i < json.length(); i++) {
            try {
                jsonObject = json.getJSONObject(i);
                    if(jsonObject.has("title"))
                     {
                    	String code = jsonObject.getString("code");
                       // String title1 = jsonObject.getString("title");
                        String startTime = jsonObject.getString("start_time");
                        String endTIme = jsonObject.getString("end_time");
                        arrayList.add(code + " - "  +startTime+ " to : "+endTIme  );
                     }     

                 
            } catch (JSONException jee) {
                Log.e("TAG", "" + jee.getMessage());
            }

        }
     stringListArray = arrayList.toArray(new String[arrayList.size()]);
     	
     //alertOkOnly("text", stringListArray.toString());
     	final CharSequence[] items = stringListArray;
     	AlertDialog.Builder builder=
         	new AlertDialog.Builder(timetable.this);
         	builder
         	.setTitle(title)
         	//.setMessage(message)
         	.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
 	            public void onClick(DialogInterface dialog, int item)
 	            {
 	            	
 	            	
 	            }
 	        })
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface d, int choice) {
                     // on OK button action
                     }
                     })
              .setNegativeButton("Cancel", null)      
         	.show();
     }
    public void alertOkOnly(String title, String message)
    {
    	AlertDialog.Builder builder=
        	new AlertDialog.Builder(timetable.this);
        	builder
        	.setTitle(title)
        	.setMessage(message)
        	.setPositiveButton("OK", null)
        	.show();
    } 
    
    }
